﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using DeutscheRemoval.Utils;
using Agos.Utils;
using System.IO;
using System.Diagnostics;
using System.Configuration;

namespace DeutscheRemoval
{
    public partial class Form1 : Form
    {
        bool started = false;
        public Form1()
        {
           InitializeComponent();
           this.ShowInTaskbar = false;
           this.WindowState = FormWindowState.Minimized;
           this.Hide();
            //added a comment
        }

        private void startToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Configurazione cf = new Configurazione();
            cf.Show();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void manualeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //string hours = ConfigUtil.GetHour();
            //string minutes = ConfigUtil.GetMinute();
            //string data = DateTime.Now.Date.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
            //AutomatedP a = new AutomatedP(ConfigUtil.GetUserLogin(), ConfigUtil.GetPasswordLogin(), data);
            //a.Connect();
            pianificataToolStripMenuItem.Enabled = false;
            manualeToolStripMenuItem.Enabled = false;
            backgroundWorker1.RunWorkerAsync();
        }
       
        //private void pianificataToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    if (pianificataToolStripMenuItem.Text.ToLower().Contains("start"))
        //    {
        //        manualeToolStripMenuItem.Enabled = false;
        //        pianificataToolStripMenuItem.Text = "Pianificata Stop";
        //        timer1.Enabled = true;
        //        timer1.Start();
        //    }
        //    else
        //    {
        //        manualeToolStripMenuItem.Enabled = true;
        //        pianificataToolStripMenuItem.Text = "Pianificata Start";
        //        timer1.Stop();
        //        timer1.Enabled = false;
        //    }
        //}

        // for automatic planification
       
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (ConfigUtil.GetHour() == DateTime.Now.Hour.ToString()
                && ConfigUtil.GetMinute() == DateTime.Now.Minute.ToString() && started == false)
            {
                started = true;
                WrappedAutomation();
                started = false;
            }
        }

        //manual automation using backgroundWorker
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            pianificataToolStripMenuItem.Enabled = false;
            notifyIcon1.Visible = true;
            notifyIcon1.ShowBalloonTip(3000, "Prestitempo automation", "Exporting begin", new ToolTipIcon());

            Debug.WriteLine(DateTime.Now.TimeOfDay);
            WrappedAutomation();
            Debug.WriteLine(DateTime.Now.TimeOfDay);
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (pianificataToolStripMenuItem.Enabled == false)
            {
                pianificataToolStripMenuItem.Enabled = true;
                manualeToolStripMenuItem.Enabled = true;
            }
            notifyIcon1.ShowBalloonTip(3000, "Prestitempo automation", "Exporting done", new ToolTipIcon());
        }

        //start automation process automatically
        //after 60 second the first tick event will be raised
        private void pianificataToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            if (pianificataToolStripMenuItem.Text.ToLower().Contains("start"))
            {
                manualeToolStripMenuItem.Enabled = false;
                pianificataToolStripMenuItem.Text = "Pianificata Stop";
                timer1.Enabled = true;
                timer1.Start(); //after 60 sec tick event will be raised
            }
            else
            {
                manualeToolStripMenuItem.Enabled = true;
                pianificataToolStripMenuItem.Text = "Pianificata Start";
                timer1.Stop();
                timer1.Enabled = false;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string inputPath = @"C:\Users\Emanuel\Desktop\emanuel.eutMarti\DeutscheRemovalDone\DeutscheRemoval\";
            string filename = @"input.xls";
            
            
            Automation au = new Automation();
            if (au.Start(ExcelUtils.DTReader(inputPath, filename)) == "OK")//excel file written
            {
                //send mail
            }
            else 
            Logger.Log("Problem at exporting", LogType.File);
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DirectoryInfo di = new DirectoryInfo(@"P:\@VPN02\SCARICO PAGAMENTI DIRETTI");
            //FileUtils.TwoLevelTraverse(di);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string testSearchPath = @"P:\@VPN02\SCARICO PAGAMENTI DIRETTI\";
            Dictionary<int,List<FileInfo>> dict = new Dictionary<int,List<FileInfo>>();
            dict = FileUtils.GetFilesDictionary(FileUtils.BuildFileList(new DirectoryInfo(testSearchPath)));

        }

        private void WrappedAutomation()
        {
            string inputFilePath = ConfigurationManager.AppSettings["InputFilePath"];
            string inputFilename = ConfigurationManager.AppSettings["InputFileName"];
            notifyIcon1.Visible = true;
            notifyIcon1.ShowBalloonTip(3000, "Prestitempo automation", "Exporting begin", new ToolTipIcon());

            Automation au = new Automation();
            if (au.Start(ExcelUtils.DTReader(inputFilePath, inputFilename)) == "OK")//excel file written
            {
                MailUtils.SendMail(ConfigurationManager.AppSettings["Sender"],
                                    ConfigurationManager.AppSettings["StatusEmail"],
                                    "AGOS IMPORTO SINGOLE RATA " + DateTime.Now.ToShortDateString(),
                                    "AGOS prelievo dell’importo della singola rata : " + DateTime.Now.ToShortDateString(),
                                     ConfigurationManager.AppSettings["OutputFilePath"] + ConfigurationManager.AppSettings["OutputFileName"]);
            }
            else
            {
                Logger.Log("Excel file not exported", LogType.Operation);
            }
        }
    }
}
